﻿module ParsingServices



open System
open System.ServiceModel
open Sources
open Features
open Scenarios
open Steps
open ParsingEvents



[<ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)>]
type public ParsingService() =    
   inherit ParsingContext()

   //do self.Start()

   let mutable _host : ServiceHost = null

   member public this.Start() =
      let name = "CRAPTASTIC"  // AppDomain.CurrentDomain.FriendlyName 
      let url = "net.pipe://localhost/forethought/" // + name
            
      _host <- new ServiceHost(this, Uri(url)); 
      _host.AddServiceEndpoint(typeof<IParsingTrigger>, NetNamedPipeBinding(), name) |> ignore
      _host.Open();

   abstract member Initialize : unit -> unit

   default this.Initialize() =
      "" |> ignore

   //interface IDisposable with
   member public this.Stop() =
      _host.Close()
   


type internal ParsingClient(name : string) as self =
   inherit ParsingContext()
   
   let url = "net.pipe://localhost/forethought/" +  "CRAPTASTIC" //name
   let address = new EndpointAddress(url)
   //let _proxy : IParsingTrigger = ChannelFactory<IParsingTrigger>.CreateChannel(new NetNamedPipeBinding(), address)

   do self.Initialize()
      
   member this.GetProxy() =      
      let proxy = ChannelFactory<IParsingTrigger>.CreateChannel(new NetNamedPipeBinding(), address)
      proxy


   member this.Initialize() =
      this.OnExecutionEnded.Add(
         fun () -> 
            let proxy = this.GetProxy()
            use disposable = proxy :?> IDisposable

            proxy.ExecutionEnded()
         )
  
      this.OnExecutionStarted.Add(
         fun () -> 
            let proxy = this.GetProxy()
            use disposable = proxy :?> IDisposable

            proxy.ExecutionStarted()
         )

      this.OnFeatureStarted.Add(
         fun (f) -> 
            let proxy = this.GetProxy()
            use disposable = proxy :?> IDisposable

            let result = proxy.FeatureStarted(f.Section)

            f.Succeded <- result.Succeded
            f.Continue <- result.Continue
         )

      this.OnFeatureEnded.Add(
         fun (f) -> 
            let proxy = this.GetProxy()
            use disposable = proxy :?> IDisposable

            let result = proxy.FeatureEnded(f.Section)

            f.Succeded <- result.Succeded
            f.Continue <- result.Continue
         )

      this.OnScenarioStarted.Add(
         fun (f) -> 
            let proxy = this.GetProxy()
            use disposable = proxy :?> IDisposable

            let result = proxy.ScenarioStarted(f.Section)

            f.Succeded <- result.Succeded
            f.Continue <- result.Continue
         )

      this.OnScenarioEnded.Add(
         fun (f) -> 
            let proxy = this.GetProxy()
            use disposable = proxy :?> IDisposable

            let result = proxy.ScenarioEnded(f.Section)

            f.Succeded <- result.Succeded
            f.Continue <- result.Continue
         )

      this.OnStepStarted.Add(
         fun (f) -> 
            let proxy = this.GetProxy()
            use disposable = proxy :?> IDisposable

            let result = proxy.StepStarted(f.Section)

            f.Succeded <- result.Succeded
            f.Continue <- result.Continue
         )

      this.OnStepEnded.Add(
         fun (f) -> 
            let proxy = this.GetProxy()
            use disposable = proxy :?> IDisposable

            let result = proxy.StepEnded(f.Section)

            f.Succeded <- result.Succeded
            f.Continue <- result.Continue
         )

      